﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Shop_ShopRevenueDetail_List : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns.Remove("ShopID");

            dt.Columns[0].ColumnName = "销售日期";
            dt.Columns[1].ColumnName = "现金总额";
            dt.Columns[2].ColumnName = "信用卡总额";
            dt.Columns[3].ColumnName = "网上支付";
            dt.Columns[4].ColumnName = "代用券总额";
            dt.Columns[5].ColumnName = "折旧券总额";
            dt.Columns[6].ColumnName = "税后价";
            dt.Columns[7].ColumnName = "BV总积分";
            dt.Columns[8].ColumnName = "Vip总额";
            dt.Columns[9].ColumnName = "折后价";
            dt.Columns[10].ColumnName = "汇款总额";            
            
            Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "ShopRevenueDetail.xls");
        }
        else
        {
            PublicFunction.GoError(ErrorNumber.NoData, "没有数据，不能导出到Excel!", "ShopRevenueDetail");
        }
    }

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        // Response.Redirect("Shop_List.aspx");

        Response.Write("<script> parent.parent.AddTab('Shop_List','','page/Shop_List.aspx');</script>");
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        double Cash = 0;            //现金总额
        double CreditCard = 0;      //信用卡总额
        double WebPay = 0;          //网上支付
        double TokenD = 0;          //代用券总额
        double TokenZ = 0;          //折旧券总额
        double Rate = 0;            //税后总额
        double SumVip = 0;          //应收VIP总额
        double SumBV = 0;           //BV总积分
        double zhejia = 0;          //折后价
        double Remit = 0;           //汇款总额

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Cash += Shove._Convert.StrToDouble(dt.Rows[i]["Cash"].ToString(), 0);
                CreditCard += Shove._Convert.StrToDouble(dt.Rows[i]["CreditCard"].ToString(), 0);
                WebPay += Shove._Convert.StrToDouble(dt.Rows[i]["WebPay"].ToString(), 0);
                TokenD += Shove._Convert.StrToDouble(dt.Rows[i]["TokenD"].ToString(), 0);
                TokenZ += Shove._Convert.StrToDouble(dt.Rows[i]["TokenZ"].ToString(), 0);
                Rate += Shove._Convert.StrToDouble(dt.Rows[i]["Rate"].ToString(), 0);
                SumVip += Shove._Convert.StrToDouble(dt.Rows[i]["SumVip"].ToString(), 0);
                SumBV += Shove._Convert.StrToDouble(dt.Rows[i]["SumBV"].ToString(), 0);
                zhejia += Shove._Convert.StrToDouble(dt.Rows[i]["zhejia"].ToString(), 0);
                Remit += Shove._Convert.StrToDouble(dt.Rows[i]["Remit"].ToString(), 0);
            }

            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'>" + Cash + "</td>");
            output.Write("<td align='center'>" + CreditCard + "</td>");
            output.Write("<td align='center'>" + WebPay + "</td>");
            output.Write("<td align='center'>" + TokenD + "</td>");
            output.Write("<td align='center'>" + TokenZ + "</td>");
            output.Write("<td align='center'>" + Rate + "</td>");
            output.Write("<td align='center'>" + zhejia + "</td>");
            output.Write("<td align='center'>" + Remit + "</td>");
            output.Write("<td align='center'>" + SumBV + "</td>");
            output.Write("<td align='center'>" + SumVip + "</td>");            
            output.Write("<td align='center'></td>");
        }
    }

    /// <summary>
    /// 获取上个页面传过来的值(string ShopID,string Day)
    /// </summary>
    private void ReadAction()
    {
        hShopID.Value = Shove._Web.Utility.GetRequest("ShopID");

        DataTable dt = new DAL.Tables.tbl_Shop().Open("", "ID=" + Shove._Convert.StrToLong(hShopID.Value,0),"");

        if (dt != null && dt.Rows.Count > 0)
        {
            WebPanel1.Header.Text = dt.Rows[0]["ShopName"] + " --收入明细";
        }
    }

    /// <summary>
    /// 梆定条件查询出来的数据
    /// </summary>
    private void BindData()
    {
        DataSet ds = null;

        long ShopID = Shove._Convert.StrToLong(this.hShopID.Value, 0);
        string SalesStart = "1900-12-12";
        string SalesEnd = "1900-12-12";

        if ((wdcStart.Value != null) && (wdcEnd.Value) != null)
        {
            DateTime dtStart = Shove._Convert.StrToDateTime("1900-12-12", "1900-12-12");        //销售统计开始时间
            DateTime dtEnd = Shove._Convert.StrToDateTime("1900-12-12", "1900-12-12");

            try
            {
                dtStart = Shove._Convert.StrToDateTime(wdcStart.Value.ToString(), "1900-12-12");
                dtEnd = Shove._Convert.StrToDateTime(wdcEnd.Value.ToString(), "1900-12-12");
            }
            catch { }

            if (dtStart > dtEnd)
            {
                Shove._Web.JavaScript.Alert(this.Page, "销售统计的开始时间不能大于结束时间!");
            }
            else
            {
                SalesStart = dtStart.ToString("yyyy-MM-dd");
                SalesEnd = dtEnd.ToString("yyyy-MM-dd");
            }
        }

        try
        {
            //AL.Procedures.P_Site_RevenueDetailList(ref ds, SalesStart, SalesEnd, ShopID);

            dt = ds.Tables[0];
        }
        catch { }

        this.g.DataSource = dt;
        this.g.DataBind();
    }

}
